package com.lanou3g.baidumusic.main.musicfragment.recommendfragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou3g.baidumusic.R;
import com.squareup.picasso.Picasso;

/**
 * Created by dllo on 17/3/25.
 */

public class RecyclerViewThreeAdapter extends RecyclerView.Adapter<RecyclerViewThreeAdapter.RecyclerViewThreeHolder> {

    private RecyclerViewThreeHolder holder;
    private Context context;
    private RecommendRecyclerViewBean bean;
    private int type;

    public RecyclerViewThreeAdapter(Context context, int type) {
        this.context = context;
        this.type = type;
        Log.d("RecyclerViewThreeAdapte", "RecyclerViewThreeAdapter is created");
        Log.d("RecyclerViewThreeAdapte", "getItemCount():" + getItemCount());
    }

    public void setRecommendThreeRecyclerViewBean(RecommendRecyclerViewBean bean) {
        this.bean = bean;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerViewThreeHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_fragment_recommend_three_rv_rv, parent, false);
        holder = new RecyclerViewThreeHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerViewThreeHolder holder, int position) {

        switch (type) {
            case 3:

                holder.textView1.setText(bean.getResult().getMix_1().getResult().get(position).getTitle());
                holder.textView2.setText(bean.getResult().getMix_1().getResult().get(position).getDesc());
                Picasso.with(context).load(bean.getResult().getMix_1().getResult().get(position).getPic()).into(holder.imageView);

                Log.d("RecyclerViewTwoAdapter", "12315" + bean.getResult().getMix_1().getResult().get(position).getDesc());

                break;
            case 4:

                holder.textView1.setText(bean.getResult().getMix_22().getResult().get(position).getTitle());
                holder.textView2.setText(bean.getResult().getMix_22().getResult().get(position).getDesc());
                Picasso.with(context).load(bean.getResult().getMix_22().getResult().get(position).getPic()).into(holder.imageView);

                break;
            case 8:

                holder.textView1.setText(bean.getResult().getMix_5().getResult().get(position).getTitle());
                holder.textView2.setText(bean.getResult().getMix_5().getResult().get(position).getDesc());
                Picasso.with(context).load(bean.getResult().getMix_5().getResult().get(position).getPic()).into(holder.imageView);

                break;
        }


    }

    @Override
    public int getItemCount() {
        if (null == bean) {
            return 0;
        }
        if (null == bean.getResult()) {
            return 0;
        }

        switch (type) {
            case 3:
                return bean.getResult() != null && bean.getResult().getMix_1().getResult().size() > 0 ? 6 : 0;

            case 4:
                return bean.getResult() != null && bean.getResult().getMix_22().getResult().size() > 0 ? 3 : 0;
            case 8:
                return bean.getResult() != null && bean.getResult().getMix_5().getResult().size() > 0 ? 6 : 0;

        }
        return 0;
    }

    public class RecyclerViewThreeHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private TextView textView1, textView2;

        public RecyclerViewThreeHolder(View itemView) {
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.item_fragment_recommend_three_rv_iv);
            textView1 = (TextView) itemView.findViewById(R.id.item_fragment_recommend_three_rv_tv1);
            textView2 = (TextView) itemView.findViewById(R.id.item_fragment_recommend_three_rv_tv2);
        }
    }
}
